package com.commerce.constellation;

import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.commerce.constellation.entity.Pairs;
import com.commerce.constellation.tools.RetrofitCall;
import com.mob.MobSDK;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.sharesdk.onekeyshare.OnekeyShare;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class XzpdActivity extends AppCompatActivity {


    private ImageView mImgXdMan;
    private ProgressBar mPbXdWeight;
    private ImageView mImgXdWoman;
    private TextView mTvXdWeight;
    private Spinner mSpZdMan;
    private Spinner mSpZdWoman;
    private TextView mTvXdInfo;
    private RatingBar mRbXdPair;
    private RatingBar mRbXdAttracted;
    private RatingBar mRbXdEnduring;
    private TextView mTvXdProposal;
    private TextView mTvXdBeCarefu;
    private String man;
    private String woman;
    private int manId;
    private int womanId;
    private Boolean b = false;
    private TypedArray ty;
    private ScrollView jieTuo;
    private String conent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xzpd);
        initView();
    }

    private void initView() {
        jieTuo = findViewById(R.id.jietuo);
        findViewById(R.id.share).setOnClickListener((v -> getShare()));
        mImgXdMan = findViewById(R.id.img_xd_man);
        mPbXdWeight = findViewById(R.id.pb_xd_weight);
        mImgXdWoman = findViewById(R.id.img_xd_woman);
        mTvXdWeight = findViewById(R.id.tv_xd_weight);
        mSpZdMan = findViewById(R.id.sp_zd_man);
        mSpZdWoman = findViewById(R.id.sp_zd_woman);
        mTvXdInfo = findViewById(R.id.tv_xd_info);
        mRbXdPair = findViewById(R.id.rb_xd_pair);
        mRbXdAttracted = findViewById(R.id.rb_xd_attracted);
        mRbXdEnduring = findViewById(R.id.rb_xd_enduring);
        mTvXdProposal = findViewById(R.id.tv_xd_proposal);
        mTvXdBeCarefu = findViewById(R.id.tv_xd_be_carefu);
        findViewById(R.id.fin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String[] stringArray = getResources().getStringArray(R.array.constellation_pd);
        ty = getResources().obtainTypedArray(R.array.constellation_icon);
        //初始化时运行一次
        mSpZdMan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                man=stringArray[(int) id];
                manId= (int) id;
                if(b){
                    getDesc();
                }
                b=true;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //初始化时运行一次
        mSpZdWoman.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                woman=stringArray[(int) id];
                womanId= (int) id ;
                getDesc();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    /**
     * 获取网络数据展示出来
     */
    private void getDesc() {
        Call<Pairs> pairsCall = RetrofitCall.postXzpdCall(this, man, woman);
        pairsCall.enqueue(new Callback<Pairs>() {
            @Override
            public void onResponse(Call<Pairs> call, Response<Pairs> response) {
                Pairs pairs = response.body();
                Pairs.ResultBean result = pairs.getResult();
                conent = result.getJieguo();
                mTvXdInfo.setText(conent);
                int xiangyue = Integer.parseInt(result.getXiangyue());
                mRbXdAttracted.setProgress(xiangyue/10);
                int tcdj = Integer.parseInt(result.getTcdj());
                mRbXdEnduring.setProgress(tcdj/10);
                int zhishu = Integer.parseInt(result.getZhishu());
                mRbXdPair.setProgress(zhishu/10);
                String lianai = result.getLianai();
                mTvXdProposal.setText(lianai);
                String zhuyi = result.getZhuyi();
                mTvXdBeCarefu.setText(zhuyi);
                String bizhong = result.getBizhong();
                mTvXdWeight.setText("配对比重："+bizhong);
                String[] split = bizhong.split(":");
                mPbXdWeight.setProgress(100-Integer.parseInt(split[1]));
                mImgXdMan.setImageResource(ty.getResourceId(manId,0));
                mImgXdWoman.setImageResource(ty.getResourceId(womanId,0));
            }

            @Override
            public void onFailure(Call<Pairs> call, Throwable t) {

            }
        });
    }

    /**
     * 分享
     */
    private void getShare(){
        OnekeyShare oks = new OnekeyShare();
        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle("星座app");
        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText(conent);
        getBitmap();
        oks.setImagePath(Environment.getExternalStorageDirectory().getPath()+"/scrollview.jpg");
        // 启动分享GUI
        oks.show(MobSDK.getContext());
    }

    /**
     *截图
     */
    private void getBitmap(){
        Bitmap bitmap;
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap( getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels,
                Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        jieTuo.draw(canvas);
        // 测试输出
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/scrollview.jpg");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            if (null != out) {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
        }
    }

}